package com.huasoft.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huasoft.domain.Right;
import com.huasoft.util.JDBConnection;

public class RightDao {
	public List<Right> getRightsByRoleId(Integer id) {
		String sql = "select rig.id,rig.name from roleright rr,rights rig "
				+ "where rr.role_id=? and rr.right_id=rig.id";
		Connection con = null;
		PreparedStatement ps = null;
		List<Right> rights = null;
		ResultSet rs = null;
		try {
			con = JDBConnection.getConnection();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs == null) {
				return null;
			}
			rights = new ArrayList<Right>();
			while (rs.next()) {
				Right right = new Right();
				right.setId(rs.getInt(1));
				right.setName(rs.getString(2));
				rights.add(right);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				JDBConnection.closeConnection(con, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println(rights);
		return rights;
	}
	
}
